//
//  SetTimeWeekView.m
//  DTSampleSocket
//
//  Created by ZhangYeKun on 2017/3/9.
//  Copyright © 2017年 Fidetro. All rights reserved.
//

#import "SetTimeWeekView.h"
@interface SetTimeWeekView ()

@end

@implementation SetTimeWeekView

CGFloat spacingWidth = 10;


- (instancetype)init{
    
    if ([super init]) {
        
        self.backgroundColor = [UIColor whiteColor];
        [self masLayoutSubview];
        
    }
    
    return self;
    
}

- (NSString *)getHexadecimalByButtonSelected{
    
    NSString *repeatStr = [NSString string];
    BOOL everyWeekRepeat = NO;
    for (NSInteger i = 6; i < [self getPublicObject].count - 1; i--) {
        UIButton *button = [self getPublicObject][i];
        repeatStr = [NSString stringWithFormat:@"%@%@",repeatStr,button.selected == YES ? @"1":@"0"];
        
        if (button.selected == YES) {
            everyWeekRepeat = YES;
        }
        if (i == 0) {
            repeatStr = [NSString stringWithFormat:@"%@%@",everyWeekRepeat == YES ? @"1":@"0",repeatStr];
        }
    }
    
    return [NSString decimalToHexadecimal:[[NSString binaryToDecimal:repeatStr]integerValue]];
}

- (NSString *)getButtonSelected{
    
    NSMutableString *repeatStr = [NSMutableString string];
    if (self.mondayButton.selected) {
        [repeatStr appendFormat:@"一  "];
    }
    
    if (self.tuesdayButton.selected) {
        [repeatStr appendFormat:@"二  "];
    }
    
    if (self.wednesdayButton.selected) {
        [repeatStr appendFormat:@"三  "];
    }
    
    if (self.thursdayButton.selected) {
        [repeatStr appendFormat:@"四  "];
    }
    
    if (self.fridayButton.selected) {
        [repeatStr appendFormat:@"五  "];
    }
    
    if (self.saturdayButton.selected) {
        [repeatStr appendFormat:@"六  "];
    }
    
    if (self.sundayButton.selected) {
        [repeatStr appendFormat:@"日  "];
    }
    
    if (![repeatStr isEqualToString:@""]) {
        NSRange deleteRange = {[repeatStr length] - 2, 2};
        [repeatStr deleteCharactersInRange:deleteRange];
    } else {
        [repeatStr appendFormat:@"永不"];
    }
    

    return repeatStr;
}

- (void)setRepeat:(NSString *)repeat {
    NSArray *array = [repeat componentsSeparatedByString:@"  "];
    for (int i = 0 ;i < array.count;i++) {
        NSString *string = array[i];
        
        if ([string isEqualToString:@"一"]) {
            self.mondayButton.selected = YES;
        }
        
        if ([string isEqualToString:@"二"]) {
            self.tuesdayButton.selected = YES;
        }
        
        if ([string isEqualToString:@"三"]) {
            self.wednesdayButton.selected = YES;
        }
        
        if ([string isEqualToString:@"四"]) {
            self.thursdayButton.selected = YES;
        }
        
        if ([string isEqualToString:@"五"]) {
            self.fridayButton.selected = YES;
        }
        
        if ([string isEqualToString:@"六"]) {
            self.saturdayButton.selected = YES;
        }
        
        if ([string isEqualToString:@"日"]) {
            self.sundayButton.selected = YES;
        }
    }

}


- (void)selectAction:(UIButton *)sender{
    
    sender.selected = !sender.selected;
    
}

- (UIButton *)mondayButton{
    
    if (!_mondayButton) {
        
        __weak UIView *superView = self;
        _mondayButton = [[UIButton alloc]init];
        [superView addSubview:_mondayButton];
        [_mondayButton setBackgroundImage:[UIImage imageNamed:@"button_week_nor"] forState:UIControlStateNormal];
        [_mondayButton setBackgroundImage:[UIImage imageNamed:@"button_week_sel"] forState:UIControlStateSelected];
        [_mondayButton setTitle:@"一" forState:UIControlStateNormal];
        [_mondayButton setTitleColor:[UIColor colorWithRed:153/255.0 green:153/255.0 blue:153/255.0 alpha:1] forState:UIControlStateNormal];
        [_mondayButton setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        [_mondayButton addTarget:self action:@selector(selectAction:) forControlEvents:UIControlEventTouchDown];
//        _mondayButton.selected = YES;
    }
    
    return _mondayButton;
    
}

- (UIButton *)tuesdayButton{
    
    if (!_tuesdayButton) {
        
        __weak UIView *superView = self;
        _tuesdayButton = [[UIButton alloc]init];
        [superView addSubview:_tuesdayButton];
        [_tuesdayButton setBackgroundImage:[UIImage imageNamed:@"button_week_nor"] forState:UIControlStateNormal];
        [_tuesdayButton setBackgroundImage:[UIImage imageNamed:@"button_week_sel"] forState:UIControlStateSelected];
        [_tuesdayButton setTitle:@"二" forState:UIControlStateNormal];
        [_tuesdayButton setTitleColor:[UIColor colorWithRed:153/255.0 green:153/255.0 blue:153/255.0 alpha:1] forState:UIControlStateNormal];
        [_tuesdayButton setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        [_tuesdayButton addTarget:self action:@selector(selectAction:) forControlEvents:UIControlEventTouchDown];
//        _tuesdayButton.selected = YES;

    }
    return _tuesdayButton;
}

- (UIButton *)wednesdayButton{
    
    if (!_wednesdayButton) {
        
        __weak UIView *superView = self;
        _wednesdayButton = [[UIButton alloc]init];
        [superView addSubview:_wednesdayButton];
        [_wednesdayButton setBackgroundImage:[UIImage imageNamed:@"button_week_nor"] forState:UIControlStateNormal];
        [_wednesdayButton setBackgroundImage:[UIImage imageNamed:@"button_week_sel"] forState:UIControlStateSelected];
        [_wednesdayButton setTitle:@"三" forState:UIControlStateNormal];
        [_wednesdayButton setTitleColor:[UIColor colorWithRed:153/255.0 green:153/255.0 blue:153/255.0 alpha:1] forState:UIControlStateNormal];
        [_wednesdayButton setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        [_wednesdayButton addTarget:self action:@selector(selectAction:) forControlEvents:UIControlEventTouchDown];
//        _wednesdayButton.selected = YES;

    }
    return _wednesdayButton;
}

- (UIButton *)thursdayButton{
    
    if (!_thursdayButton) {
        
        __weak UIView *superView = self;
        _thursdayButton = [[UIButton alloc]init];
        [superView addSubview:_thursdayButton];
        [_thursdayButton setBackgroundImage:[UIImage imageNamed:@"button_week_nor"] forState:UIControlStateNormal];
        [_thursdayButton setBackgroundImage:[UIImage imageNamed:@"button_week_sel"] forState:UIControlStateSelected];
        [_thursdayButton setTitle:@"四" forState:UIControlStateNormal];
        [_thursdayButton setTitleColor:[UIColor colorWithRed:153/255.0 green:153/255.0 blue:153/255.0 alpha:1] forState:UIControlStateNormal];
        [_thursdayButton setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        [_thursdayButton addTarget:self action:@selector(selectAction:) forControlEvents:UIControlEventTouchDown];
//        _thursdayButton.selected = YES;

        
    }
    return _thursdayButton;
}

- (UIButton *)fridayButton{
    
    if (!_fridayButton) {
        
        __weak UIView *superView = self;
        _fridayButton = [[UIButton alloc]init];
        [superView addSubview:_fridayButton];
        [_fridayButton setBackgroundImage:[UIImage imageNamed:@"button_week_nor"] forState:UIControlStateNormal];
        [_fridayButton setBackgroundImage:[UIImage imageNamed:@"button_week_sel"] forState:UIControlStateSelected];
        [_fridayButton setTitle:@"五" forState:UIControlStateNormal];
        [_fridayButton setTitleColor:[UIColor colorWithRed:153/255.0 green:153/255.0 blue:153/255.0 alpha:1] forState:UIControlStateNormal];
        [_fridayButton setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        [_fridayButton addTarget:self action:@selector(selectAction:) forControlEvents:UIControlEventTouchDown];
//        _fridayButton.selected = YES;

    }
    return _fridayButton;
}

- (UIButton *)saturdayButton{
    
    if (!_saturdayButton) {
        
        __weak UIView *superView = self;
        _saturdayButton = [[UIButton alloc]init];
        [superView addSubview:_saturdayButton];
        [_saturdayButton setBackgroundImage:[UIImage imageNamed:@"button_week_nor"] forState:UIControlStateNormal];
        [_saturdayButton setBackgroundImage:[UIImage imageNamed:@"button_week_sel"] forState:UIControlStateSelected];
        [_saturdayButton setTitle:@"六" forState:UIControlStateNormal];
        [_saturdayButton setTitleColor:[UIColor colorWithRed:153/255.0 green:153/255.0 blue:153/255.0 alpha:1] forState:UIControlStateNormal];
        [_saturdayButton setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        [_saturdayButton addTarget:self action:@selector(selectAction:) forControlEvents:UIControlEventTouchDown];
//        _saturdayButton.selected = YES;

    }
    return _saturdayButton;
    
}

- (UIButton *)sundayButton{
    
    if (!_sundayButton) {
        
        __weak UIView *superView = self;
        _sundayButton = [[UIButton alloc]init];
        [superView addSubview:_sundayButton];
        [_sundayButton setBackgroundImage:[UIImage imageNamed:@"button_week_nor"] forState:UIControlStateNormal];
        [_sundayButton setBackgroundImage:[UIImage imageNamed:@"button_week_sel"] forState:UIControlStateSelected];
        [_sundayButton setTitle:@"日" forState:UIControlStateNormal];
        [_sundayButton setTitleColor:[UIColor colorWithRed:153/255.0 green:153/255.0 blue:153/255.0 alpha:1] forState:UIControlStateNormal];
        [_sundayButton setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        [_sundayButton addTarget:self action:@selector(selectAction:) forControlEvents:UIControlEventTouchDown];
//        _sundayButton.selected = YES;

    }
    return _sundayButton;
    
}

- (UILabel *)tipLabel{
    
    if (!_tipLabel) {
        
        __weak UIView *superView = self;
        _tipLabel = [[UILabel alloc]init];
        [superView addSubview:_tipLabel];
        _tipLabel.text = @"重复/每周";
        [_tipLabel setFont:[UIFont systemFontOfSize:px1080Width(48)]];

    }
    
    return _tipLabel;
    
}

- (void)masLayoutSubview{
    CGFloat spacingHeight = px1920Height(28);
    __weak UIView *superView = self;
    [self.mondayButton mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.left.mas_equalTo(superView.mas_left).mas_offset(spacingWidth);
        make.bottom.mas_equalTo(superView.mas_bottom).mas_offset(-spacingHeight);
        make.height.mas_equalTo(self.mondayButton.mas_width);
        
    }];
    [self.tuesdayButton mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.left.mas_equalTo(self.mondayButton.mas_right).mas_offset(spacingWidth);
        make.bottom.mas_equalTo(superView.mas_bottom).mas_offset(-spacingHeight);
        make.width.height.mas_equalTo(self.mondayButton);
        
        
    }];
    [self.wednesdayButton mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.left.mas_equalTo(self.tuesdayButton.mas_right).mas_offset(spacingWidth);
        make.bottom.mas_equalTo(superView.mas_bottom).mas_offset(-spacingHeight);
        make.width.height.mas_equalTo(self.mondayButton);
    }];
    [self.thursdayButton mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(self.wednesdayButton.mas_right).mas_offset(spacingWidth);
        make.bottom.mas_equalTo(superView.mas_bottom).mas_offset(-spacingHeight);
        make.width.height.mas_equalTo(self.mondayButton);
        
    }];
    [self.fridayButton mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(self.thursdayButton.mas_right).mas_offset(spacingWidth);
        make.bottom.mas_equalTo(superView.mas_bottom).mas_offset(-spacingHeight);
        make.width.height.mas_equalTo(self.mondayButton);
        
    }];
    [self.saturdayButton mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.left.mas_equalTo(self.fridayButton.mas_right).mas_offset(spacingWidth);
        make.bottom.mas_equalTo(superView.mas_bottom).mas_offset(-spacingHeight);
        make.width.height.mas_equalTo(self.mondayButton);
        
    }];
    [self.sundayButton mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.left.mas_equalTo(self.saturdayButton.mas_right).mas_offset(spacingWidth);
        make.right.mas_equalTo(superView.mas_right).mas_offset(-spacingWidth);
        make.bottom.mas_equalTo(superView.mas_bottom).mas_offset(-spacingHeight);
        make.width.height.mas_equalTo(self.mondayButton);
        
    }];
    [self.tipLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(superView.mas_top).mas_offset(px1920Height(48));
        make.left.mas_equalTo(superView.mas_left).mas_offset(px1080Width(48));
        
    }];
    
    
    UIView *line1 = [[UIView alloc] init];
    [superView addSubview:line1];
    line1.backgroundColor = [UIColor colorWithHexString:@"#D0D3DC"];
    [line1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(superView);
        make.width.equalTo(superView);
        make.height.mas_equalTo(1);
    }];
    
    UIView *line2 = [[UIView alloc] init];
    [superView addSubview:line2];
    line2.backgroundColor = [UIColor colorWithHexString:@"#D0D3DC"];
    [line2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(superView.mas_centerY);
        make.width.equalTo(superView);
        make.height.mas_equalTo(1);
    }];
    
    UIView *line3 = [[UIView alloc] init];
    [superView addSubview:line3];
    line3.backgroundColor = [UIColor colorWithHexString:@"#D0D3DC"];
    [line3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(superView);
        make.width.equalTo(superView);
        make.height.mas_equalTo(1);
    }];
    
}



@end
